<?php

namespace app\index\controller;

use think\Controller;
use think\Loader;
use app\index\model\Share as ShareModel;

class Share extends Controller
{
    public function create()
    {
        $validate = Loader::validate('CreateShare');
        if(!$validate->check($_GET)){
            if($validate->getError() !== true){
                echoJson(0, $validate->getError());
            }
        }
        $_GET["create_time"] = date("Y-m-d H:i:s");
        echoJson(1, "添加成功",ShareModel::create($_GET));
    }

    public function index()
    {
        $creamId = $_GET["cream_id"];
        $result = ShareModel::Where("cream_id",$creamId)->order("create_time desc")->select();
        echoJson(1,"查询成功",$result);
    }

    public function read($id)
    {
        $result = ShareModel::where("id",$id)->select();
        if(empty($result)){
            $result = null;
        }else{
            $result = $result[0];
        }
        echoJson(1,"查询成功",$result);
    }

    public function update($id)
    {
        $updateData = $this->request->param();
        ShareModel::Where("id",$id)->update($updateData);
        echoJson(1,"更新成功");
    }

    public function delete($id)
    {
        ShareModel::Where("id",$id)->delete();
        echoJson(1,"删除成功");
    }

}